package com.joekai.view_demo.zdy_text;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.joekai.view_demo.R;
import com.joekai.view_demo.utils.PxUtils;

/**
 * 自定义TextView
 *
 * @author joekai
 */
public class ZdyTextView extends View {
    final static String TAG = "-------ZdyTextView";
    //显示文字
    private final String mTextStr;
    //文字大小
    private int mTextSize = 15;
    //文字颜色
    private int mTextColor = Color.BLACK;
    //文字显示模式
    private final int mOrientation;
    //水平显示
    private final static int HORIZONTAL = 0;
    //垂直显示
    private final static int VERTICAL = 1;
    //文字长度
    private int textLength = 0;
    //画笔
    private final Paint mPaint;
    //计算的宽度 与 字体的长度有关  与字体的大小  用画笔来测量
    private final Rect textRect;

    public ZdyTextView(Context context) {
        this(context, null);
    }

    public ZdyTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZdyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public ZdyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        //获取自定义Attrs属性
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ZdyTextView);
        mTextStr = array.getString(R.styleable.ZdyTextView_text);
        mTextSize = array.getDimensionPixelSize(R.styleable.ZdyTextView_textSize, PxUtils.sp2px(mTextSize));
        mTextColor = array.getColor(R.styleable.ZdyTextView_textColor, mTextColor);
        mOrientation = array.getInt(R.styleable.ZdyTextView_orientation, HORIZONTAL);
        //回收
        array.recycle();

        mPaint = new Paint();
        //抗锯齿
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(mTextSize);
        mPaint.setColor(mTextColor);

        textRect = new Rect();

    }

    protected void setTextColor(int textColor) {
        this.mTextColor = textColor;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //获取宽高的模式
        //获取宽高的大小
        int widgetSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        switch (MeasureSpec.getMode(widthMeasureSpec)) {
            //给的是wrap_content  需要计算
            case MeasureSpec.AT_MOST:

                //获取文本的Rect
                mPaint.getTextBounds(mTextStr, 0, mTextStr.length(), textRect);
                //垂直
                if (mOrientation == VERTICAL) {
                    String[] strings = mTextStr.split("\n");
                    for (String s : strings) {
                        textLength = s.length();
                    }
                    heightSize = textRect.height() * textLength + getPaddingTop() + getPaddingBottom();
                    widgetSize = mTextSize * strings.length + getPaddingLeft() + getPaddingRight();
                } else {
                    heightSize = textRect.height() + getPaddingTop() + getPaddingBottom();
                    widgetSize = textRect.width() + getPaddingLeft() + getPaddingRight();
                }
                break;
            case MeasureSpec.EXACTLY:
            case MeasureSpec.UNSPECIFIED:
            default:
                break;

        }
        //设置控件的宽高
        setMeasuredDimension(widgetSize, heightSize);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //dy代表的是：高度的一半到baseLine的距离
        //获取文字排版信息
        Paint.FontMetricsInt fontMetricsInt = mPaint.getFontMetricsInt();
        int dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 - fontMetricsInt.bottom;
        //垂直
        if (mOrientation == VERTICAL) {
            int baseLine = getHeight() / 2 + dy - mTextSize * textLength / 2;
            String[] strings = mTextStr.split("\n");
            for (int j = 0; j < strings.length; j++) {
                for (int i = 0; i < strings[j].length(); i++) {
                    String s = strings[j].substring(i, i + 1);
                    canvas.drawText(s, (getPaddingLeft() + j * mTextSize), (baseLine + i * mTextSize + getPaddingTop()), mPaint);
                }
            }
        } else {
            int baseLine = getHeight() / 2 + dy;
            canvas.drawText(mTextStr, getPaddingLeft(), baseLine, mPaint);
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            //手指移动
            case MotionEvent.ACTION_MOVE:
                Log.e(TAG, "手指移动");
                break;
            //手指按下
            case MotionEvent.ACTION_DOWN:
                Log.e(TAG, "手指按下");
                break;
            //手指抬起
            case MotionEvent.ACTION_UP:
                Log.e(TAG, "手指抬起");
                break;
            default:
                break;
        }
        return true;
    }
}
